{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sqlalchemy import create_engine\n",
    "engine = create_engine('sqlite:///db.sql', echo=True)\n",
    "\n",
    "from sqlalchemy.ext.declarative import declarative_base\n",
    "Base = declarative_base()\n",
    "\n",
    "from sqlalchemy import Column, Integer, String\n",
    "class Post(Base):\n",
    "    __tablename__ = 'posts'\n",
    "    id = Column(Integer, primary_key=True)\n",
    "    topic_content = Column(String)\n",
    "    post_content = Column(String)\n",
    "    post_authar = Column(String)\n",
    "    \n",
    "    def __repr__(self):\n",
    "        return \"<Post(topic_content='%s', post_content='%s', post_authar='%s')>\" % (self.topic_content, self.post_content, self.post_authar)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-11-24 02:38:03,373 INFO sqlalchemy.engine.base.Engine PRAGMA table_info(\"posts\")\n",
      "2017-11-24 02:38:03,374 INFO sqlalchemy.engine.base.Engine ()\n"
     ]
    }
   ],
   "source": [
    "Base.metadata.create_all(engine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sqlalchemy.orm import sessionmaker\n",
    "Session = sessionmaker(bind=engine)\n",
    "session = Session()\n",
    "test_post = Post(topic_content='XXX', post_content='XXX', post_authar='XXX')\n",
    "session.add(test_post)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<sqlalchemy.orm.query.Query at 0x10414d050>"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_post = session.query(Post)\n",
    "test_post"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sqlalchemy.orm import sessionmaker\n",
    "Session = sessionmaker(bind=engine)\n",
    "session = Session()\n",
    "session.add_all([\n",
    "    Post(topic_content='XXX', post_content='XXX 2', post_authar='XXX'),\n",
    "    Post(topic_content='XXX', post_content='XXX 3', post_authar='XXX'),\n",
    "    Post(topic_content='XXX', post_content='XXX 4', post_authar='XXX')])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-11-24 02:38:24,869 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)\n",
      "2017-11-24 02:38:24,872 INFO sqlalchemy.engine.base.Engine SELECT posts.id AS posts_id, posts.topic_content AS posts_topic_content, posts.post_content AS posts_post_content, posts.post_authar AS posts_post_authar \n",
      "FROM posts ORDER BY posts.id\n",
      "2017-11-24 02:38:24,873 INFO sqlalchemy.engine.base.Engine ()\n"
     ]
    }
   ],
   "source": [
    "from sqlalchemy.orm import sessionmaker\n",
    "Session = sessionmaker(bind=engine)\n",
    "session = Session()\n",
    "for instance in session.query(Post).order_by(Post.id):\n",
    "    print(instance.topic_content, instance.post_content)\n",
    "    #print(instance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
